package com.example.nhlictapp;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		String username = "heid1306@student.nhl.nl";
		String password = "Doradehond!!2";
		
		WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		
		WifiConfiguration wc = new WifiConfiguration();
		wc.hiddenSSID = false;
		wc.SSID = "\"eduroam\"";
		wc.priority = 99;
		
		
		/*Key Mgmnt*/
	    wc.allowedKeyManagement.clear();
	    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
	    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

	    /*Group Ciphers*/
	    wc.allowedGroupCiphers.clear();
	    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
	    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
	    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
	    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
	    //dd
 
	    /*Pairwise ciphers*/
	    wc.allowedPairwiseCiphers.clear();
	    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
	    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

	    /*Protocols*/
	    wc.allowedProtocols.clear();
	    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
	    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
		
		WifiEnterpriseConfig wec = new WifiEnterpriseConfig();
		wec.setIdentity(username);
		wec.setPassword(password);
		wec.setEapMethod(1);
		wec.setPhase2Method(3);
		wc.enterpriseConfig = wec;
		
		
		
		
		
    	int res = wifi.addNetwork(wc);//WifiManager.addNetwork(WifiConfiguration config) adds new network description to set of configured networks.
    	
    	
    	
    	
    	Toast.makeText(getApplicationContext(), "Netwerk toevoegen returnde: " + res, Toast.LENGTH_SHORT).show();
    	
    	boolean b = wifi.enableNetwork(res, true);
    	Toast.makeText(getApplicationContext(), "Netwerk activeren returnde: " + b, Toast.LENGTH_SHORT).show();
    	wifi.saveConfiguration();
    	
    	Toast.makeText(getApplicationContext(), "Werkt!", Toast.LENGTH_SHORT).show();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}
